$(function(){
    var admin_id = getCookie('admin_id');
    if(!admin_id){
        window.location.replace('login.html');
    }
    var elem = document.querySelector('.js-switch');
    var switchery = new Switchery(elem, {
        color: '#1AB394'
    });
    //业务开始
    var id = getQueryString('id');
    if(!id){
        swal("操作错误", "参数传递错误！", "error");
        setTimeout(function(){
            location.href="taocan_list.html";
        },3000);
        return false;
    }

    //获取基本信息
    $.ajax({
        url: ApiUrl + "/goods/"+id,
        type: 'get',
        dataType: 'JSON',
        success: function(result) {
            if(result.code == 200){
                document.getElementById("article_no").value = result.data.article_no;
                document.getElementById("bar_code").value = result.data.bar_code;
                document.getElementById("zh_name").value = result.data.zh_name;
                document.getElementById("en_name").value = result.data.en_name;
                document.getElementById("mnemonic").value = result.data.mnemonic;
                document.getElementById("brand").value = result.data.brand;
                document.getElementById("tax").value = result.data.tax;
                document.getElementById("price").value = result.data.price;
                document.getElementById("inventory").value = result.data.inventory;
                if(result.data.status == 1){
                    $("#site_status1").attr("checked","checked");
                    $("#site_status0").removeAttr("checked");
                    $(".cb-enable").addClass('selected');
                    $(".cb-disable").removeClass('selected');
                }else{
                    $("#site_status0").attr("checked","checked");
                    $("#site_status1").removeAttr("checked");
                    $(".cb-enable").removeClass('selected');
                    $(".cb-disable").addClass('selected');
                }
                $("#category_id").val(result.data.category_id);
            }else{
                parent.layer.msg(result.message);
                return false;
            }
        }
    });
    //自定义radio样式
    $(".cb-enable").click(function(){
        var parent = $(this).parents('.onoff');
        $('.cb-disable',parent).removeClass('selected');
        $(this).addClass('selected');
        $('.checkbox',parent).attr('checked', true);
    });
    $(".cb-disable").click(function(){
        var parent = $(this).parents('.onoff');
        $('.cb-enable',parent).removeClass('selected');
        $(this).addClass('selected');
        $('.checkbox',parent).attr('checked', false);
    });
    //提交表单
    $("#submit").click(function(){
        if($("#signupForm").valid()){
            var query = $("#signupForm").serialize();
            ajax_post(ApiUrl + "/goods/category/"+id,"category.html",query);
        }
    });
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            name: {
                required: true
            }
        },
        messages: {
            article_no: {
                required: icon+ "请输入分类名称！"
            }
        }
    });
});